

var mysql  = require('mysql');

// let mysql = import mysql;

// import mysql from 'mysql';

/*
var connection = mysql.createConnection({
    host     : '192.168.1.212',
    user     : 'root',
    password : 'handkoo',
    port: '3306',
    database: 'test'
});

connection.connect();

var  sql = 'SELECT * FROM usertype';
//查
connection.query(sql,function (err, result) {
    if(err){
        console.log('[SELECT ERROR] - ',err.message);
        return;
    }

    console.log('--------------------------SELECT----------------------------');
    console.log(result);
    console.log('------------------------------------------------------------\n\n');
});

connection.end();

*/


var pool = mysql.createPool({
    connectionLimit:2,//连接池最多可以创建连接数
    host:'192.168.1.212',
    user:'root',
    database:'test',
    password:'handkoo',
    queueLimit:8 // 队伍中等待连接的最大数量，0为不限制。
});

// pool.query('select * from person',function(err,rows,fileds){
//     console.log(rows);
// });

//创建一个连接，超过connectionLimit，会调用下面 'enqueue' 等待队列
pool.on('connection',function(){
    console.log('创建一个连接');
});

// 超过连接池连接数量 进入等待队列
pool.on('enqueue',function(){
    console.log('进入等待队列');
});
function query(){
    pool.getConnection(function(err,connection){

        var  sql = 'SELECT * FROM usertype';

        connection.query(sql,function(err,result){

            console.log('--------------------------SELECT----------------------------');
            console.log(result);
            console.log('------------------------------------------------------------\n\n');

            setTimeout(function(){
                console.log('releaseconnection');
                connection.release();
            },200)
        })
    })
};
query();//入队
query();//创建一个连接
query();//创建一个连接